package tool.dialog;

import java.awt.Frame;

import javax.swing.JComponent;
import javax.swing.JFrame;

import assister.ClassAssister;

import tool.MainConstructorNotFoundException;
import tool.classloader.ClassNotSupportedException;

/**rappresenta un JConstructorDialog che è in grado di utilizzare anche
 * JField del tipo JComponentFixedField
 * 
 * @author koldar
 * @version 1.1
 */
public final class JConstructorUpgradedDialog extends JConstructorDialog{

	private static final long serialVersionUID = 4642034942157240673L;
	
	private JComponent defaultValue;

	public JConstructorUpgradedDialog(Frame parent, Class<?> _toconstruct,JComponent fixedvalue)throws MainConstructorNotFoundException {
		super(parent, _toconstruct,false);
		this.defaultValue=fixedvalue;
		try {
			this.generateViewModel();
		} catch (NullPointerException | ClassNotSupportedException error) {
			new ErrorDialog(parent,error.getMessage());
			this.dispose();
		}
	}

	@Override
	protected JField retrieveCorrectTextField(Class<?> param) throws ClassNotSupportedException{
		try{
			JField<?> result=super.retrieveCorrectTextField(param);
			return result;
		} catch (ClassNotSupportedException error){
			if ((param==JComponent.class)||(ClassAssister.isSuperclass(JComponent.class, param))){//se param è un estensione di JComponent
				return new JComponentFixedField(param,this.defaultValue);
			}
			throw new ClassNotSupportedException(param);
		}
	}

	
}
